//
//  AnyWidthPagingCollectionView.m
//  TRoad2
//
//  Created by ZhangYanhua on 14/11/13.
//  Copyright (c) 2014年 Mach. All rights reserved.
//

#import "LeftPagingFlowCollectionLayout.h"

@implementation LeftPagingFlowCollectionLayout

- (void)awakeFromNib
{
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.minimumInteritemSpacing = 1.0f;
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
                                 withScrollingVelocity:(CGPoint)velocity
{
    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);

    NSArray *array = [super layoutAttributesForElementsInRect:targetRect];
    
    UICollectionViewLayoutAttributes *firstItemAttr = [array firstObject];
    
    if (firstItemAttr) {
        if (firstItemAttr.center.x >= proposedContentOffset.x) {
            return CGPointMake(firstItemAttr.frame.origin.x, proposedContentOffset.y);
        } else {
            return CGPointMake(CGRectGetMaxX(firstItemAttr.frame) + self.minimumInteritemSpacing, proposedContentOffset.y);
        }
    }

    return proposedContentOffset;
}

@end
